﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ElectronicRecords.Models
{
    public class RequestsRepository
    {
        private ElectronicRecordsDataContext _datacontext;
        public RequestsRepository (ElectronicRecordsDataContext d)
        {
            _datacontext = d;
        }

        public Requests GetRequest(int id)
        {
            return _datacontext.Requests.SingleOrDefault(r=>r.ID_Request==id);
        }

        public IEnumerable<Requests> GetAllRequests()
        {
            return _datacontext.Requests.AsEnumerable<Requests>();
        }

        //не оптимально
        public IEnumerable<Requests> GetRequestsBetweenDate(DateTime begin, DateTime end)
        {
            return GetAllRequests().Where(r => CompareBetweenDate(begin, end, r.DateRequest)).AsEnumerable<Requests>();
        }
        public IEnumerable<Requests> GetRequestsBetweenDateBySpec(DateTime begin, DateTime end, int specid)
        {
            return GetAllRequests().Where(r => CompareBetweenDate(begin, end, r.DateRequest) && r.Procedure_Specialist_XREF.ID_SPECIALIST==specid).AsEnumerable<Requests>();
        }
         private bool CompareBetweenDate(DateTime time1, DateTime time2, DateTime timei)
        {

            if (DateTime.Compare(time1, timei) <= 0 && DateTime.Compare(time2, timei) >= 0)
                return true;
            else return false;

        }

         public bool CreateRequest(Requests r)
         {
             _datacontext.Requests.InsertOnSubmit(r);
             _datacontext.SubmitChanges();
             return true;
         }

    }
}